package com.xusheng.cli.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.shareModule.Share;
import com.xusheng.cli.service.ShareService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * (Share)表控制层
 *
 * @author whr
 * @since 2023-11-21 13:52:13
 */
@Validated
@RestController
@RequestMapping("/api/v1/share")
public class ShareController {
    /**
     * 服务对象
     */
    @Resource
    private ShareService shareService;

    /**
     * 查询所有数据
     *
     * @param share 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @GetMapping("queryAll")
    public Result<List<Share>> queryAll(Share share) {
        return Result.success(shareService.list(new QueryWrapper<>(share)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @param share 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Share>> queryByPage(MyPage<Share> page, Share share) {
        return Result.success(shareService.page(page, new QueryWrapper<>(share)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @GetMapping("queryOne/{id}")
    public Result<Share> selectOne(@PathVariable Serializable id) {
        return Result.success(shareService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param share 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Share share) {
        return Result.isSuccess(shareService.save(share));
    }

    /**
     * 修改数据
     *
     * @param share 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody Share share) {
        return Result.isSuccess(shareService.updateById(share));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        Share share = new Share();
        share.setId(id);
        return Result.isSuccess(shareService.removeById(share));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-11-21 13:52:13
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(shareService.batchDelete(idList, new Share()));
    }

    @GetMapping("getShareConfig")
    public Result<Share> getShareConfig() {
        List<Share> list = shareService.list();
        if (list.size() > 0) {
            return Result.success(list.get(0));
        } else {
            return Result.success();
        }
    }

    @PostMapping("setShareConfig")
    public Result<Object> setShareConfig(@Validated(Save.class) @RequestBody Share share) {
        return Result.isSuccess(shareService.saveOrUpdate(share));
    }
}

